package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.utils.ToolsUtils;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyShopAlbumCate;
import zhihuilan.mapper.MyShopAlbumCateMapper;
import zhihuilan.params.query.MyShopAlbumCateQueryParam;
import zhihuilan.service.MyShopAlbumCateService;

import java.util.List;

@Service
public class MyShopAlbumCateServiceImpl extends ServiceImpl<MyShopAlbumCateMapper, MyShopAlbumCate> implements MyShopAlbumCateService {

    @Override
    public WxMiniPageDto<MyShopAlbumCate> selectPageList(MyShopAlbumCateQueryParam queryParam) {
        QueryWrapper<MyShopAlbumCate> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getTitle())) {
            queryWrapper.eq("title", queryParam.getTitle());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        queryWrapper.orderByAsc("sort");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyShopAlbumCate> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyShopAlbumCate> result = new WxMiniPageDto(page.getCurrent(), page.getSize());
        BeanUtils.copyProperties(page, result);
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public List<MyShopAlbumCate> selectList(MyShopAlbumCateQueryParam queryParam) {
        QueryWrapper<MyShopAlbumCate> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getTitle())) {
            queryWrapper.eq("title", queryParam.getTitle());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        queryWrapper.orderByAsc("sort");
        List<MyShopAlbumCate> selectList = this.baseMapper.selectList(queryWrapper);
        return selectList;
    }

    @Override
    public MyShopAlbumCate selectById(Integer id) {
        MyShopAlbumCate result = this.baseMapper.selectById(id);
        return result;
    }

    @Override
    public MyShopAlbumCate addEntity(MyShopAlbumCate entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public MyShopAlbumCate updateEntity(MyShopAlbumCate entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public Boolean deletetById(Integer id) {
        int result = this.baseMapper.deleteById(id);
        return result > 0 ? true : false;
    }

    @Override
    public Integer getMaxSort(Integer shopId) {
        if (ObjectUtils.isEmpty(shopId)) {
            return 0;
        }
        QueryWrapper<MyShopAlbumCate> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(sort) as sort");
        queryWrapper.eq("shopid", shopId);
        MyShopAlbumCate sort = this.baseMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(sort)) {
            return 0;
        }
        return sort.getSort();
    }
}
